package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户管理接口数据
 * @Author: luog
 * @Date: 2024/11/27
 */
@RestController
@RequestMapping("/users/data/")
@Api(tags = "用户管理接口数据")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getAllUsers")
    @ApiOperation(value = "获取所有用户")
    public List<User> getAllUsers() {
        return userService.list();
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "新增用户")
    public Boolean addUser(@RequestBody User user) {
        Long id = user.getId();
        if (id == null) {
            return userService.save(user);
        }else{
            return userService.updateById(user);
        }
    }


    @GetMapping("/getUserById/{id}")
    @ApiOperation(value = "根据ID获取用户")
    public User getUserById(@PathVariable Long id) {
        return userService.getById(id);
    }

    @PutMapping("/updateUser/{id}")
    @ApiOperation(value = "更新用户")
    public void updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        userService.updateById(user);
    }

    @GetMapping("/deleteUser/{id}")
    @ApiOperation(value = "删除用户")
    public Map deleteUser(@PathVariable Long id) {
        userService.removeById(id);
        Map res = new HashMap();
        res.put("ok", "ok");
        return res;
    }

}
